﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class RhombusGridComponent : MonoBehaviour
{
    bool _EnableBlock = false;

    public void EnableGizmos(bool flag)
    {
        _EnableBlock = flag;

        if (_blocks == null)
            return;

        for (int i = 0; i < _blocks.Length; ++i)
        {
            RhombusBlockComponent rbc = _blocks[i].GetComponent<RhombusBlockComponent>();
            if (rbc != null)
                rbc.SetEnable(flag);
        }
    }

    //  网格
    public Vector3[] _grids = null;

    public GameObject[] _blocks = null;

    public nRhombusVBOData _Data;

    /// <summary>
    /// 内切菱形
    /// </summary>
    /// <param name="h_diagonal">水平对角线</param>
    /// <param name="v_diagonal">垂直对角线</param>
    public void FormatInscribed(int n, nRhombusVBOData data)
    {
        _Data = data;

        {
            float sec_dis = data._dis / n;

            int totalnumber = n * n;

            List<Vector3> list = new List<Vector3>(totalnumber);

            int col = 0;
            int row = 0;

            for (row = 0; row < n; ++row)
            {
                for (col = 0; col < n; ++col)
                {
                    Vector3 x = data._dir_dn * row * sec_dis;
                    Vector3 y = data._dir_up * col * sec_dis;
                    Vector3 pos = x + y + data._left / 2;

                    list.Add(pos);
                }
            }

            _grids = list.ToArray();


        }

        EnableGizmos(_EnableBlock);

#if !UNITY_EDITOR
        MeshTemplate();

        List<GameObject> listGos = new List<GameObject>();

        for (int i = 0; i < _Data._grids.Length; ++i)
        {
            GameObject o = GameObject.Instantiate(_meshTem);
            o.transform.position += new Vector3(0, 0, -5);
            o.name = i.ToString();
            o.transform.parent = gameObject.transform;
            o.transform.localPosition = _Data._grids[i] - _Data._left / _Data._sec;
            o.layer = gameObject.layer;
            //o.layer = ZoneManager.CameraBlockLayer;
            RhombusBlockComponent rbc = o.AddComponent<RhombusBlockComponent>();
            rbc.SetEnable(false);
            listGos.Add(o);
        }

        StaticBatchingUtility.Combine(listGos.ToArray(), gameObject);

        _blocks = listGos.ToArray();
#endif
    }

    public nRectangleVBOData _RectData;

    public void FormatRectangle(nRectangleVBOData data)
    {
        _RectData = data;

        int row = Mathf.RoundToInt(_RectData._width / ZoneCfg.RhombusMin);
        float xx = ZoneCfg.RhombusMin / ZoneCfg.RhombusFactor / 2;
        int col = Mathf.RoundToInt(_RectData._height / xx);

        List<Vector3> list = new List<Vector3>();

        for (int j = 0; j < col; ++j)
        {
            for (int i = 0; i < row; ++i)
           {
                //  偶数行
                if (j % 2 == 0)
                {
                    float x = i * ZoneCfg.RhombusMin + ZoneCfg.RhombusMin / 2;
                    float y = (j / 2) * ZoneCfg.RhombusMin / ZoneCfg.RhombusFactor;
                    float z = 0;

                    Vector3 p = new Vector3(x, y, z);
                    list.Add(p);
                }
                //  奇数行
                else if (j % 2 == 1)
                {
                    float x = i * ZoneCfg.RhombusMin;
                    float y = ((j - 1) / 2) * ZoneCfg.RhombusMin / ZoneCfg.RhombusFactor + ZoneCfg.RhombusMin / ZoneCfg.RhombusFactor / 2;
                    float z = 0;

                    Vector3 p = new Vector3(x, y, z);
                    list.Add(p);
                }
            }
        }

        _grids = list.ToArray();

        EnableGizmos(_EnableBlock);
    }

    public void FormatRectangleMesh()
    { 

        MeshTemplate();

        List<GameObject> listGos = new List<GameObject>();

        for (int i = 0; i < _grids.Length; ++i)
        {
            GameObject o = GameObject.Instantiate(_meshTem);
            MeshRenderer r = o.GetComponent<MeshRenderer>();
            Material materialColored = MaterialUtil.PickMat();
            materialColored.color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 0.1f);
            r.material = materialColored;
            o.layer = ZoneCfg.CameraWorldLayer;
            o.transform.position += new Vector3(0, 0, 1);
            o.name = i.ToString();
            o.transform.parent = gameObject.transform;
            o.transform.localScale = new Vector3(0.95f, 0.95f, 0);
            o.transform.position = gameObject.transform.position + _grids[i];// - new Vector3(ZoneCfg.RhombusMin / 2, 0, 0);
            o.layer = gameObject.layer;
            //o.layer = ZoneManager.CameraBlockLayer;
            RhombusBlockComponent rbc = o.AddComponent<RhombusBlockComponent>();
            rbc._Order = i;
            rbc.SetEnable(false);
            listGos.Add(o);
        }

        StaticBatchingUtility.Combine(listGos.ToArray(), gameObject);

        _blocks = listGos.ToArray();
    }

    public void FreshBlock(List<int> list, ref int[] _grid_blocks)
    {
        if (_blocks == null)
            return;

        for (int i = 0; i < _blocks.Length; ++i)
        {
            RhombusBlockComponent rbc = _blocks[i].GetComponent<RhombusBlockComponent>();

            if (_grid_blocks[i] == 1)
                rbc.SetStatus(1);

            //  可以放置 绿色
            if (_grid_blocks[i] == 0)
                rbc.SetStatus(2);

            if (list.Contains(i))
            {
                //  不可放置 红色
                if (_grid_blocks[i] == 1)
                    rbc.SetStatus(4);
                else
                    rbc.SetStatus(3);
            }
        }
    }

#region 模板
    static GameObject _meshTem = null;
    /// <summary>
    /// 新建一个模板
    /// </summary>
    static Material MeshTemplate()
    {
        if (_meshTem == null)
        {
            //  创建一个单元数据
            nRhombusVBOData temData = new nRhombusVBOData(ZoneCfg.RhombusMin, ZoneCfg.RhombusMin / ZoneCfg.RhombusFactor);

            _meshTem = new GameObject("template");

            MeshFilter mf = _meshTem.AddComponent<MeshFilter>();
            MeshRenderer mr = _meshTem.AddComponent<MeshRenderer>();

            Vector3[] vertices = new Vector3[6];
            int[] triangles = new int[6];

            //forward
            vertices[0].Set(temData._left.x, temData._left.y, temData._left.z);
            vertices[1].Set(temData._top.x, temData._top.y, temData._top.z);
            vertices[2].Set(temData._right.x, temData._right.y, temData._right.z);
            vertices[3].Set(temData._bottom.x, temData._bottom.y, temData._bottom.z);

            int currentCount = 0;
            for (int i = 0; i < 4; i = i + 4)
            {
                triangles[currentCount++] = i;
                triangles[currentCount++] = i + 1;
                triangles[currentCount++] = i + 2;
        
                triangles[currentCount++] = i;
                triangles[currentCount++] = i + 2;
                triangles[currentCount++] = i + 3;
        
            }

            mf.mesh.vertices = vertices;
            mf.mesh.triangles = triangles;

            //Material materialColored = MaterialUtil.PickMat();
            //materialColored.color = new Color(Random.Range(0, 1), Random.Range(0, 1), Random.Range(0, 1), 1);
            //mr.material = materialColored;
        }


        return null;
    }
#endregion
}
